#!/usr/bin/env bash
# Copyright 2016 GSI, Inc. All rights reserved.
#
#
location=$1
echo "DDS from this location is used ${location}"
source ${location}/DDS_env.sh

eval DDS_OCTOPUS_LOCATION="$DDS_LOCATION/tests/dds-octopus"
TOPO_1=$DDS_OCTOPUS_LOCATION/octopus_topo_1.xml
TOPO_1_NUMBER_OF_AGENTS=10

usage() {
    cat <<EOF
DDS test utility
   Copyright 2016 GSI, Inc. All rights reserved.

Usage: $(basename $0) [OPTION]
     -n 			Number of tasks to deploy.

Report bugs to http://dds.gsi.de
EOF
}

clean_up()
{
	dds-session stop
	
	exit $1
}
#=============================================================================
# ***** OPTARG *****
#=============================================================================
#NUMBER_OF_AGENT=0
#while getopts "h:n:" opt; do
#  case $opt in
#  	n)
#    	NUMBER_OF_AGENT=$OPTARG
#		;;
#  	h) 
#    usage
#    exit 0
#    ;;
#  \?)
#    echo "Invalid option: -$OPTARG" >&2
#    exit 1
#    ;;
#  esac
#done

#if [ "$NUMBER_OF_AGENT" -eq 0]; then
#	echo "Error: Number of agents is required. Use -n X, where X is the desired number of agents."
#	exit 1
#fi

echo "Starting DDS commander server..."
dds-session start --local
if (( $? != 0 )) ; then
	echo "Failed to start DDS commander server. Exit code: $?"
	clean_up 1
fi

echo "waiting for commander server to start..."
dds-info --status $> /dev/null
COUNT=0
# max 10 sec.
MAX_COUNT=100
while [ $? -ne 0 ]; do
	if [ $COUNT -gt $MAX_COUNT ]; then
		echo "Error: Timeout is reached."
		clean_up 1
	fi
	sleep 0.1
	COUNT=$(expr $COUNT + 1)
	dds-info --status $> /dev/null
done
dds-info --status

echo "Submitting [$TOPO_1_NUMBER_OF_AGENTS] agents..."
dds-submit -r localhost -n $TOPO_1_NUMBER_OF_AGENTS
if (( $? != 0 )) ; then
	echo "Failed to submit agents. Exit code: $?"
	clean_up 1
fi

COUNT=0
# max 30 sec.
MAX_COUNT=300
AGENTS_COUNT=$(dds-info -n)
while [ true ]; do
	if [ $COUNT -gt $MAX_COUNT ]; then
		echo "Error: Timeout is reached."
		clean_up 1
	fi
	sleep 0.1
	COUNT=$(expr $COUNT + 1)
	AGENTS_COUNT=$(dds-info -n)
	if [ $AGENTS_COUNT -eq $TOPO_1_NUMBER_OF_AGENTS ]; then
		echo "Number of agents online: $AGENTS_COUNT"
		break
	fi
done

echo "Agents are ready."

echo "Setting up the topology and activating it..."

dds-topology --activate "$TOPO_1"
if (( $? != 0 )) ; then
	echo "Failed to activate topology. Exit code: $?"
	clean_up 1
fi

echo "START TEST"

$DDS_OCTOPUS_LOCATION/dds-octopus -n 10
RESULT=$?

clean_up $RESULT
